﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using log4net;

namespace Entangle
{
    class RequestService :IRequestService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(RequestService));

        public string SendRequestXML(string xml)
        {
            Message message;
            Message response;
            try
            {
                message = Message.FromXML(xml);
                response = MessageManager.Instance.ProcessMessage(message);
            }
            catch (IdentityVerificationException ie)
            {
                log.Error("Exception while parsing message from xml", ie);
                response = new Message("Your message was not signed properly. Only messages from recognised partner are processed." + Environment.NewLine +
                    "Your attempt has been logged. Please refrain from further unauthorised access attempts", MessageType.UNAUTHORISED);
            }
            catch (MessageException me)
            {
                log.Error("Exception while parsing message from xml", me);
                response = new Message(me.Message, MessageType.MESSAGE_MALFORMED);
            }
            return response.ToXMLString();
        }
    }
}
